/**
 * Created by lixiaodi on 2018/10/10.
 */
var states = [{code: 1, text: '开启' }, {code: 2, text: '关闭'}];

mini.parse();
var form = new mini.Form("form1");
var createDateVal = mini.getByName('createDate');
//页面初始化
$(function(){
	//得到系统当前的时间
	var nowDate = new Date();
	//格式化时间
	nowDate = formatDate(nowDate);
	console.log(nowDate);
	//设置创建的值
	createDateVal.setValue(nowDate);
})
//保存
function SaveData() {
	//验证
	form.validate();
    if (form.isValid() == false) return;
    //得到数据
    var ids = form.getData();
    //校验：用户名不能为空
//    if(ids.modularName){
    	var modular = {
    	        modularId : ids.modularId,
    	        modularName : ids.modularName,
    	        modularLcons : ids.modularLcons,
    	        modularState: ids.modularState,
    	        modularCode : ids.modularCode,
				modularOrderId : ids.modularOrderId,
    	        modularUrl: ids.modularUrl,
    	        createDate : ids.createDate
    	    };
    	    console.log(modular,'要保存的新建数据');
    	    modular = JSON.stringify(modular);
    	    console.log(modular);
    	    $.ajax({
    	        url: ajaxUrl + "/modular/insertModular.do",
    	        type: 'POST',
    	        data: modular,
    	        contentType : "application/json",
    	        success: function (data) {
    	        	console.log("123");
    	            CloseWindow("save");
    	        },
    	        error: function (jqXHR, textStatus, errorThrown) {
    	            //alert(jqXHR.responseText);
					console.log("789");
    	            CloseWindow();
    	        }
    	    });
//    }else{
//    	mini.alert('用户名不能为空');
//    }
}
//编辑/新增
function SetData(data) {
	//编辑
    if (data.action == "edit") {
        //跨页面传递的数据对象，克隆后才可以安全使用
        data = mini.clone(data);
        var modularId = data.id;
        $.ajax({
            url: ajaxUrl + "/modular/findByModularsId.do?modularId=" + modularId,
            type: 'POST',
            success: function (data) {
            	console.log(data);
                var o = mini.decode(data);
                console.log(o);
                //日期格式转化
                if(o.data.createDate){
                	 o.data.createDate = formatDate(o.data.createDate);
                }else{
                	o.data.createDate = '';
                }
                //表单赋值
                form.setData(o.data);
                form.setChanged(false);
            },
            error:function(data){
                mini.alert('失败');
            }
        });
    //新增
    }else if(data.action == "new"){
    	//新增时模块状态默认开启
        var modularState = mini.getByName('modularState');
        modularState.setValue(states[0].code);
    }
}

function GetData() {
    var o = form.getData();
    return o;
}
//关闭子页面
function CloseWindow(action) {
	console.log(action,'关闭子页面时候的action');
    if (action == "close" && form.isChanged()) {
        if (confirm("数据被修改了，是否先保存？")) {
            return false;
        }
    }
    if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
    else window.close();
}
function onOk(e) {
    SaveData();
}
function onCancel(e) {
    CloseWindow("cancel");
}
//校验模块名称是否重复
function findByModularName(){
	var ids = form.getData();
	var modularName = ids.modularName;
	console.log(modularName);
	$.ajax({
		type    : 'POST',
		url     : ajaxUrl + '/modular/findModularName.do',
		data    : {
			modularName : modularName
		},
		success : function(data){
			console.log(data);
			if(data.state == 'error'){
				mini.alert(data.message);
				mini.get('modularName').setValue(''); 
			}
		},
		error   : function(message){
			mini.alert(message);
		}
	})
}
//校验模块编码是否重复
function findByModularCode(){
	var ids = form.getData();
	var modularCode = ids.modularCode;
	
	$.ajax({
		type 	: 'POST',
		url  	: ajaxUrl + '/modular/findModularCode.do',
		data 	: {
			modularCode : modularCode
		},
		success : function(data){
			console.log(data);
			if(data.state == 'error'){
				mini.alert(data.message);
				mini.get('modularCode').setValue(''); 
			}
		},
		error   : function(message) {
			mini.alert(message);
		}
	})
}
//校验模块排序号是否重复
function findByModularOrderId() {
	var ids = form.getData();
	var modularOrderId = ids.modularOrderId;
	console.log(modularOrderId);
	$.ajax({
		type:'POST',
		url:ajaxUrl + '/modular/findByModularOrderId.do',
		data:{
			modularOrderId:modularOrderId
		},
		success:function (data) {
			console.log(data);
			if(data.state='error'){
				mini.alert(data.message);

				mini.get('modularOrderId').setValue('');
			}
		},
		error:function (message) {
			mini.alert(message);
		}

	})
}
//校验模块必输
function onNotSpaceValidation(e) {
    if (e.isValid) {
        if (!e.value) {
            e.errorText = "不能为空";
            e.isValid = false;
        }
    }
}
